<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>欢迎页</title>
    <link rel="stylesheet" href="../../static/frame/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/frame/static/css/style.css">
    <link rel="icon" href="../../static/frame/static/image/code.png">
</head>
<body class="body">

<div class="layui-row layui-col-space10 my-index-main">
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label" for="demoReload">选择套餐</label>
        <div class="layui-input-block">
            <select id="demoReload" name="fid" lay-search required lay-verify="required">

            </select>
        </div>
    </div>

</form>
    <div id="body">
        <form class="layui-form" action="" style="">
            <div class="layui-form-item">
                <label class="layui-form-label">搜索</label>
                <div class="layui-input-block">
                    <input id="content" autocomplete="off" placeholder="请输入卡密" class="layui-input" type="text">
                    <br>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="search">搜索卡密</button>
                    <hr class="layui-bg-blue">
                </div>

            </div>

        </form>

        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">生成数量</label>
                <div class="layui-input-block">
                    <input type="text" name="num" required lay-verify="required" placeholder="生成卡密数量" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="createkey">生成卡密</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    <hr class="layui-bg-blue">
                </div>

            </div>
        </form>
        <blockquote class="layui-elem-quote">注意：清空和删除卡密不管卡密是否已使用，一律不归还使用的余额！</blockquote>
        <div class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">卡密操作</label>
                <div class="layui-input-block">
                    <button class="layui-btn" onclick="Kami('ExportNoUse')">导出未使用卡密</button>
                    <button class="layui-btn" onclick="Kami('ExportAll')">导出全部卡密</button>
                    <button class="layui-btn" onclick="Kami('DelNoUse')">清空未使用卡密</button>
                    <button class="layui-btn" onclick="Kami('DelNoUse')">清空已使用卡密</button>
                    <button class="layui-btn" onclick="Kami('DelAll')">清空全部卡密</button>
                </div>
            </div>

        </div>
        <table class="layui-hide" id="keylist" lay-filter="keylist"></table>
    </div>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>

</div>


<script src="../../static/jquery-3.3.1.js"></script>
<script type="text/javascript" src="../../static/frame/layui/layui.js"></script>
<script type="text/javascript" src="../../static/js/index.js"></script>
<script type="text/javascript" src="../../static/frame/echarts/echarts.min.js"></script>
<script type="text/javascript">
    $('#body').hide();

    layui.use(['form', 'table', 'element', 'layer', 'vip_tab'], function () {
        // 打开选项卡
        var table = layui.table, form = layui.form;
        window.table = table;
        $('.my-nav-btn').on('click', function () {
            if ($(this).attr('data-href')) {
                vipTab.add($(this), '<i class="layui-icon">' + $(this).find("button").html() + '</i>' + $(this).find('p:last-child').html(), $(this).attr('data-href'));
            }
        });



        var load = layer.load();
        $.ajax({
            url: '../ajax.php?mod=fidlist_show',
            type: 'POST',
            dataType: 'html',
            data: '',
            success: function (data) {
                layer.close(load);
                $('#demoReload').html(data);
                form.render('select');
            },
            error: function (data) {
                layer.close(load);
                layer.msg('请求失败' + data);
            }
        });
        table.render({
            elem: '#keylist'
            , cellMinWidth: 130
            , cols: [[
                {field: 'ID', title: 'ID', width: 60}
                , {field: 'kami', title: '卡密'}
                , {field: 'user', title: '用户'}
                , {field: 'creattime', title: '生成时间'}
                , {field: 'usetime', title: '使用时间'}
                , {field: 'status', title: '状态'}
                , {toolbar: '#barDemo', title: '操作', width: 240}
            ]]
            , page: true
        });

        form.on('select', function(data){
            if (data.value != null && data.value !== '请选择' && data.value!=='') {
                $('#body').show();window.fid=data.value;
                table.reload('keylist', {
                    url: '../ajax.php?mod=gettckeylist'
                    , where: {'fid':fid} //设定异步数据接口的额外参数
                });
            }else{
                $('#body').hide()
            }

        });
        window.layer = layui.layer;


        //监听性别操作
        form.on('switch', function (obj) {

            $.ajax({
                url: '../ajax.php?mod=tckeystatus',
                type: 'POST',
                dataType: 'html',
                data: 'keyid=' + this.value + '&status=' + obj.elem.checked,
                success: function (data) {
                    layer.msg(data);
                },
                error: function (data) {
                    layer.msg('请求失败' + data);
                }
            });
        });

        table.on('tool', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url: '../ajax.php?mod=deltckey',
                        type: 'POST',
                        dataType: 'html',
                        data: 'keyid=' + obj.data.ID,
                        success: function (data) {
                            obj.del();
                            layer.msg(data);
                        },
                        error: function (data) {
                            layer.msg('请求失败' + data);
                        }
                    });
                    layer.close(index);
                });
            }
        });

        form.on('submit(search)', function (data) {
            //alert('111');
            table.reload('keylist', {
                url: '../ajax.php?mod=gettckeylist'
                , where: {'search': $('#content').val(),'fid':fid} //设定异步数据接口的额外参数
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        form.on('submit(createkey)', function (data) {
            var loading = layer.load();
            var fid = $('#demoReload').val();
            $.ajax({
                url: '../ajax.php?mod=creatfidkey',
                type: 'POST',
                dataType: 'json',
                data: 'sc_count=' + data.field.num + '&sc_fid=' + fid,
                success: function (data) {
                    layer.close(loading);
                    if (data.code === '1') {
                        table.reload('keylist', {
                            url: '../ajax.php?mod=gettckeylist'
                        });
                        layer.confirm(data.keys, {
                            btn: ['下载卡密', '关闭'] //按钮
                        }, function () {
                            window.open('../ajax.php?mod=download&sign=' + data.sign);
                        });
                    } else {
                        layer.msg(data.msg);
                    }

                },
                error: function (data) {
                    layer.close(loading);
                    layer.msg('请求失败' + data);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    });

    function Kami(operation) {
        var fid = $('#demoReload').val();
        layer.confirm('确定要执行操作：' + operation + '？', function (index) {
            $.ajax({
                url: '../ajax.php?mod=FidKeyOperation',
                type: 'POST',
                dataType: 'html',
                data: 'operation=' + operation+'&fid='+fid,
                success: function (data) {
                    table.reload('keylist', {
                        url: '../ajax.php?mod=gettckeylist'
                    });
                    layer.msg(data);
                },
                error: function (data) {
                    layer.msg('请求失败' + data);
                }
            });
            layer.close(index);
        });

    }
</script>
</body>
</html>